<template>
  <a-drawer :title="`运单`" width="70%" :visible="visible"
            hide-cancel
            @cancel="visible = false"
            :footer="false"
            v-if="visible">
    <ma-crud ref="crudRef" :columns="columns" :options="shipmentOptions">
      <template #tableBeforeButtons>
        <a-button type="primary" status="" @click="changeRoute" v-auth="['document:shipment:changeRoute']">
          <icon-edit/>
          修改路由
        </a-button>
        <a-button type="primary" status="warning" @click="backRoute" v-auth="['document:shipment:changeRoute']">
          <icon-edit/>
          恢复路由
        </a-button>
      </template>
      <template #operationBeforeExtend="{ record }">
        <a-link @click="routeList(record)" v-if="record.name == '提单'" v-auth="['document:waybill:delete']"> <icon-list/> 路由</a-link>
        <a-popconfirm
            content="确定删除路由提单吗?"
            position="bottom"
            @click.stop=""
            @ok="deleteWaybill(record)"
            >
          <a-link status="danger" v-if="record.name == '提单'" v-auth="['document:waybill:delete']"><icon-delete/> 删除</a-link>
      </a-popconfirm>
      </template>
    </ma-crud>
    <batchActionModal :crud-ref="crudRef" :selected="selected" ref="batchActionRef"/>
    <add-modal ref="addModalRef"></add-modal>
    <Route ref="route" />
  </a-drawer>
</template>
<script setup>
import {computed, inject, ref,watch,reactive,h} from "vue";
import parcel from "@/api/document/parcel.js";
import BatchActionModal from "@cps/actions/batchActionModal.vue";
import shipment from '@/api/document/shipment'
import waybill from '@/api/document/waybill'
import AddModal from "@cps/actions/addModal.vue";
import Route from "@/views/document/waybill/ShipmentRouteListInfo.vue";
import dictHelp from "@/utils/dicthelp";
import {getDictData} from "@/utils/crud/dict";
const crudRef = ref()
const batchActionRef = ref()
const record = inject('record')
const selected = computed(() => crudRef.value?.selecteds)
const visible = ref(false)
const addModalRef = ref()
const route = ref()
const waybill_id = ref(0)
const onShow = (id)=>{
  visible.value = true
  waybill_id.value = id
}

let shipmentOptions = ref({
      rowSelection: {showCheckedAll: true},
      pageLayout: 'fixed',
      // 默认统一设置列宽度
      columnWidth: 0,
      tableLayoutFixed: true,
      api: parcel.waybillShipment,
      expandAllRows: true,
      isExpand: true,
      operationColumn: true,
      operationColumnAlign: 'center',
      operationColumnWidth: 120,
      beforeRequest: (requestParams) => {
        requestParams.waybill_id = record.value.id
        requestParams.id = waybill_id.value
      },
    })
const columns = reactive([
  {
      title: "单号",
      dataIndex: "shipment_no",
      formType: "input",
      search: true,
  },
  {
      title: "单据类型",
      dataIndex: "name",
      formType: "input",
  },
  {
    title: "地址编码",
    dataIndex: "address.address_lib_detail_id",
    formType: "select",
    search: true,
    dict: dictHelp.addressLibDetail
  },
  {
    title: "用户",
    dataIndex: "user_id",
    formType: "select",
    search: true,
    dict: dictHelp.customer
  },
  {
    title: "服务",
    dataIndex: "service_id",
    formType: "select",
    search: true,
    virtualListProps:{},
    dict: dictHelp.service
  },
  {
    title: "箱数",
    dataIndex: "shipment_waybill_parcel_count",
    customRender: ({record}) => {
          return record.shipment_waybill ?
                   parseInt(record.shipment_waybill.parcel_count) == parseInt(record.volume_weight.parcel_count) ? 
                    record.volume_weight.parcel_count : 
                    h('span',{style:{color:'red'}}, parseInt(record.shipment_waybill.parcel_count) + '/'+ parseInt(record.volume_weight.parcel_count))
                  : '' 
                
      },
  },
  {
    title: "实重",
    dataIndex: "shipment_waybill_parcel_weight",
    customRender: ({record}) => {
          return record.shipment_waybill ?
                   parseFloat(record.shipment_waybill.weight) == parseFloat(record.volume_weight.actual_weight) ? 
                    record.volume_weight.actual_weight : 
                    h('span',{style:{color:'red'}}, record.shipment_waybill.weight + '/'+ record.volume_weight.actual_weight)
                  : '' 
                
      },
  },
  {
    title: "体积",
    dataIndex: "shipment_waybill_parcel_volume",
    customRender: ({record}) => {
          return record.shipment_waybill ?
                   parseFloat(record.shipment_waybill.volume) == parseFloat(record.volume_weight.actual_volume) ? 
                    record.volume_weight.actual_volume : 
                    h('span',{style:{color:'red'}}, record.shipment_waybill.volume + '/'+ record.volume_weight.actual_volume)
                  : '' 
                
      },
  },
  {
    title: "报关方式",
    dataIndex: "clearance.exportwith",
    formType: "select",
    dict: {name:'Exportwith',translation:true}
  },
  {
    title: "清关关方式",
    dataIndex: "clearance.importwith",
    formType: "select",
    dict: {name:'Importwith',translation:true}
  },
  {
    title: "属性",
    dataIndex: "attrs",
    customRender: ({record}) => {
      return getDictData('attrs',record.attrs,crudRef)
    }
  },
  {
    title: "主品名",
    dataIndex: "volume_weight.main_name",
  },
  {
    title: "货站",
    dataIndex: "depot_id",
    formType: "select",
    dict:dictHelp.depot,
  },
  {
      title: "拣货时间",
      dataIndex: "picking_at",
      formType: "date",
      showTime: true
  },
  {
      title: "下单时间",
      dataIndex: "created_at",
      formType: "date",
      showTime: true
  },
])

const backRoute = () =>{
  batchActionRef.value.open(
    {
        title: '恢复路由', api: shipment.changeRoute, form: {
          key: 'shipment_id',
          form:{
            route_about_waybill_user:record.value.id
          },
          columns: [],
          submited: async () => {
            crudRef.value.refresh();
          }
        }
      },
    )
}

const changeRoute = () => {
  batchActionRef.value.open(
    {
        title: '修改路由', api: shipment.changeRoute, form: {
          key: 'shipment_id',
          columns: [
            {
              title: "客户展示路由",
              dataIndex: "route_about_waybill_user",
              formType: "select",
              dict:dictHelp.waybillChildren(record.value.id),
              addApi: () => {
                addModalRef.value.open(
                [
                  {
                    title: "提单号",
                    required: true,
                    dataIndex: "number",      
                  },
                  {
                    dataIndex: "departure",
                    addDisplay: false,
                    addDefaultValue:-1
                  },
                  {
                    dataIndex: "destination",
                    addDisplay: false,
                    addDefaultValue:-1
                  },
                  {
                    dataIndex: "parent_id",
                    addDisplay: false,
                    addDefaultValue:record.value.id
                  },
                ]  
                , {
                  viewType: 'modal',
                }, waybill.saveSon)
              },
              addApiAuth:'document:waybill:saveSon',
            },
          ],
          submited: async () => {
            crudRef.value.refresh();
          }
        }
      },
    )
}
const deleteWaybill = async (info) =>{
  let res = await waybill.deletes({ids:[info.id]})
  crudRef.value.refresh()
}
const routeList = (info) =>{
  route.value.onShow(info.id)
}
defineExpose({onShow})
</script>